/*  
 *  ------ [DM_05] - get RSSI from last received packet  -------- 
 *  
 *  Explanation: This program shows how to get RSSI value from the 
 *  last received packet. For this protocol, there is an API function which
 *  shows this information. Before running this example, make sure there 
 *  is another emitter sending packets to this XBee module in order to 
 *  receive information.
 *
 *  Copyright (C) 2012 Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details. 
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 *  
 *  Version:           0.1 
 *  Design:            David Gascón 
 *  Implementation:    Yuri Carmona
 */

#include <WaspXBeeDM.h>

// variable to store RSSI
long rssi;


void setup()
{  
  // init USB port
  USB.ON();
  USB.println(F("DM_05 example"));

  // init XBee 
  xbeeDM.ON(); 

}


void loop()
{

  // check available data in RX buffer
  if( xbeeDM.available() > 0 ) 
  {
    // parse information
    xbeeDM.treatData(); 

    // check RX flag after 'treatData'
    if( !xbeeDM.error_RX ) 
    {
      // check available packets
      while( xbeeDM.pos>0 )
      {  
        USB.println(F("\n\nNew packet received:")); 
        USB.println(F("---------------------------"));

        // get RSSI signal and make conversion to -dBm
        xbeeDM.getRSSI();  
        if( xbeeDM.error_AT == 0 )
        {
          rssi=xbeeDM.valueRSSI[0];    
          rssi*=-1;
          USB.print(F("RSSI(dBm): "));
          USB.println(rssi,DEC);
        }   
        else
        {
          USB.println(F("AT command error"));
        }

        // free memory
        free(xbeeDM.packet_finished[xbeeDM.pos-1]);
        xbeeDM.packet_finished[xbeeDM.pos-1]=NULL; 

        // decrement the received packet counter
        xbeeDM.pos--; 
      }
    } 
  }

  delay(5000);
}








